#include "console.hpp"

#include <cstdio>
#include <cstdlib>
#include <cstring>

namespace canyon::device{

void Console::call(uint8_t cmd, uint64_t){
    switch(cmd){
        case 1:{
            uint8_t * tmpstr = (uint8_t *)malloc(sizeof(uint8_t)*this->memory.max_size());

            fgets(reinterpret_cast<char *>(tmpstr), this->memory.max_size(), stdin);
            for(size_t i = 0; i<=strlen(reinterpret_cast<char *>(tmpstr)); i++){
                this->memory.set(i, tmpstr[i]);
            }

            free(tmpstr);
            break;
        }
        case 0:
            for(size_t i = 0; i<this->memory.max_size(); i++){
                uint8_t c = this->memory.get(i);
                if(c == '\0') break;
                fputc(c, stdout);
            }
            break;
        default:
            break;
    }
}

}
